<?php

namespace App\Http\Model;

use App\Http\Services\ComeBackServices;
use App\Lib\Snowflake\SnowflakeGenerator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceJd extends Base
{
    protected $table = 'finance_jd_stream';
    protected $ticketLog = 'new_year_20240118_stream';//人气票记录表
    protected $giftBag;

    public function __construct(array $attributes = [])
    {
        $this->tablePrefix = env('DB_PREFIX');
        parent::__construct($this->table, $attributes);
        $this->giftBag = new GiftBag();
    }


    /**
     * 更新用户金豆
     * @param int $userId 更新该user_id
     * @param int $dataType 数据类型 1=商品SKUID 2=道具ID 3=礼物ID 4=全家总动员期数 5=西天取经ID 6=代理金豆转入记录ID 7=装备拍卖记录ID 8=开运装备星订单ID 9=双旦节抽奖ID 10=回归活动
     * @param int $dataId 数据ID
     * @param int $jdType 类型 1=商品购买获得 2=道具购买赠送 3=全家总动员奖励赠送 4=解除代理-获得金豆 5=代理转入-获得金豆 6=装备拍卖-获得金豆 7=装备拍卖-分成金豆 8=装备拍卖-系统后台-赠送金豆 9=金币兑换-获得金豆 10=西天取经-抽奖获得金豆 11=西天取经-悬赏获得金豆 12=双旦节-获得金豆  13=年度盛典-获得金豆  14=活动榜单-获得金豆 15=回归活动-获得金豆
     *                        51=购买礼物扣除 52=全家总动员押注扣除 53=西天取经用户抽奖扣除金豆 54=装备拍卖-支付金豆 55=开运装备星抽奖扣除金豆 56=荒漠捕牛金豆兑换幸运券 57=双旦节-支付金豆
     * @param int $changeValue 变动值 支出使用负数
     * @param string $changeDesc 变动描述
     * @return bool
     */
    public function inc($userId, $dataId, $jdType, $changeValue, $changeDesc){
        if(empty($userId)) {
            return false;
        }
        $dataType = 0;
        if(in_array($jdType, [1])) {
            $dataType = 1;
        }elseif(in_array($jdType, [2])) {
            $dataType = 2;
        }elseif(in_array($jdType, [51])) {
            $dataType = 3;
        }elseif(in_array($jdType, [3, 52])) {
            $dataType = 4;
        }elseif(in_array($jdType, [53, 10, 11])) {
            $dataType = 5;
        }elseif(in_array($jdType, [5])) {
            $dataType = 6;
        }elseif(in_array($jdType, [6,7,54,8])) {
            $dataType = 7;
        }elseif(in_array($jdType, [55])) {
            $dataType = 8;
        }elseif(in_array($jdType, [56])) {
            $dataType = 3;
        }elseif(in_array($jdType, [12])) {
            $dataType = 9;
        }elseif(in_array($jdType, [15])){
            $dataType = 10;
        }

        if(in_array($jdType, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])) {
            $changeValue = abs($changeValue);
        }else{
            $changeValue = 0 - abs($changeValue);
        }
        //装备分成下分成金额为0时候不入库处理
        if($jdType == 7 && $changeValue == 0){
            return true;
        }
        //增加娱乐值
        if(in_array($jdType, [1, 5]) && $changeValue > 0) {
            DB::table("finance_wallet")
                ->where('user_id', '=', $userId)
                ->increment('game_balance', $changeValue, ['change_timestamp'=> time()]);
        }

        DB::table("finance_wallet")
            ->where('user_id', '=', $userId)
            ->increment('jd_balance', $changeValue, ['change_timestamp'=> time()]);

        DB::table($this->table)->insert([
            "user_id" => $userId,
            "change_value" => $changeValue,
            "change_desc" => $changeDesc,
            "jd_type" => $jdType,
            "data_id" => $dataId,
            "data_type" => $dataType,
            "change_timestamp" => time()
        ]);

        Redis::DEL($this->redis_cache_finance_user_wallet . ':' . $userId);
        
        if($changeValue < 0){
            //回归活动任务处理(只能实例化这种方式引用,若加到__construct中会死循环)
            $comeBackService = new ComeBackServices();
            $comeBackService->comeBackeMissionStream($userId, 2);//金豆消耗
        }
        

        return true;
    }

    //金豆明细
    public function getStreamList($userId, $page, $page_size)
    {
        //只看最近15天的
        $timeSt = time() - 15*86400;
        $fields = ['stream_id', 'user_id', 'change_value', 'change_desc', 'change_timestamp'];
        $list = DB::table($this->table)
            ->where('user_id', $userId)
            ->where('change_timestamp', '>', $timeSt)
            ->orderBy('change_timestamp', 'desc')
            ->offset(($page) * $page_size)
            ->limit($page_size)
            ->get($fields);
        $list = self::objToArray($list);

        return $list;
    }

}
